﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Thor.Common;
using Thor.Domain;
using Thor.WebRole;
using Thor.WebRole.Services;

namespace Thor.Silverlight.Views
{
    public partial class NewCalendar : ChildWindow
    {
        private TimeZoneDomainContext _timeZoneDomainContext = null;
        private TimeDomainContext _timeDomainContext = null;
        private List<Time> _times = null;

        public NewCalendar()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(NewCalendar_Loaded);
        }

        private void NewCalendar_Loaded(object sender, RoutedEventArgs e)
        {
            LoadCalendar();
            LoadTimes();
        }

        private void newCalendarForm_ContentLoaded(object sender, DataFormContentLoadEventArgs e)
        {
            LoadTimeZones();
            EnablePublicCanBookCheckBox(false);
            EnableTimeframeComboBoxes(false);
        }

        private void LoadTimeZones()
        {
            var timeZoneComboBox = GetTimeZoneComboBox();

            if (timeZoneComboBox != null)
            {
                _timeZoneDomainContext = new TimeZoneDomainContext();
                timeZoneComboBox.ItemsSource = _timeZoneDomainContext.TimeZones;
                _timeZoneDomainContext.Load(_timeZoneDomainContext.GetTimeZonesQuery()).Completed += new EventHandler(TimeZoneDomainContext_Completed);
            }
        }

        private void TimeZoneDomainContext_Completed(object sender, EventArgs e)
        {
            var timeZoneComboBox = GetTimeZoneComboBox();

            if (timeZoneComboBox != null)
                timeZoneComboBox.SelectedIndex = 0;
        }

        private void LoadTimes()
        {
            _timeDomainContext = new TimeDomainContext();
            _timeDomainContext.Load(_timeDomainContext.GetTimesQuery()).Completed += new EventHandler(LoadTimes_Completed);
        }

        private void LoadTimes_Completed(object sender, EventArgs e)
        {
            _times = (from time in _timeDomainContext.Times
                      select time).ToList();
        }

        private void LoadStartTimes()
        {
            var startTimeComboBox = GetStartTimeComboBox();

            if (startTimeComboBox != null && _times != null)
            {
                var startTimes = (from time in _times
                                  where time.Value >= 1 && time.Value <= 47
                                  select time).ToList();

                startTimeComboBox.ItemsSource = startTimes;
                startTimeComboBox.SelectedIndex = 0;
            }
        }

        private void LoadEndTimes(int startTime)
        {
            var endTimeComboBox = GetEndTimeComboBox();

            if (endTimeComboBox != null && _times != null)
            {
                var endTimes = (from time in _times
                                where time.Value >= startTime && time.Value <= 47
                                select time).ToList();

                endTimeComboBox.ItemsSource = endTimes;
                endTimeComboBox.SelectedIndex = 0;
            }
        }

        private void LoadCalendar()
        {
            this.Calendar = new Thor.Domain.Calendar();
            this.newCalendarForm.CurrentItem = this.Calendar;
        }

        public Thor.Domain.Calendar Calendar { get; set; }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            CleanUpCalendarTimeframe();

            this.newCalendarForm.CommitEdit();
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void CleanUpCalendarTimeframe()
        {
            if (Calendar.UseTimeframe && (!Calendar.StartTime.HasValue || !Calendar.EndTime.HasValue))
            {
                Calendar.UseTimeframe = false;
                Calendar.StartTime = null;
                Calendar.EndTime = null;
            }

            if (!Calendar.UseTimeframe && (Calendar.StartTime.HasValue || Calendar.EndTime.HasValue))
            {
                Calendar.StartTime = null;
                Calendar.EndTime = null;
            }
        }

        private void OnPublicCanViewChecked(object sender, RoutedEventArgs e)
        {
            EnablePublicCanBookCheckBox(true);
        }

        private void OnPublicCanViewUnchecked(object sender, RoutedEventArgs e)
        {
            this.Calendar.PublicCanBook = false;
            EnablePublicCanBookCheckBox(false);
        }

        private void OnUseTimeframeChecked(object sender, RoutedEventArgs e)
        {
            LoadStartTimes();
            EnableTimeframeComboBoxes(true);
        }

        private void OnUseTimeframeUnchecked(object sender, RoutedEventArgs e)
        {
            ResetTimeframeComboBoxesItemsSource();
            EnableTimeframeComboBoxes(false);
        }

        private void StartTimeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var startTimeComboBox = GetStartTimeComboBox();

            Calendar.StartTime = null;
            Calendar.StartTimeDisplay = string.Empty;

            if (startTimeComboBox != null)
            {
                if (startTimeComboBox.SelectedItem != null && startTimeComboBox.SelectedItem is Time)
                {
                    Time time = (Time)startTimeComboBox.SelectedItem;

                    var selectedTime = GetSelectedTime(time.Value);

                    if (selectedTime != null)
                        Calendar.StartTimeDisplay = selectedTime.Text;

                    Calendar.StartTime = time.Value;

                    int startTimeValue = time.Value + 1;
                    LoadEndTimes(startTimeValue);
                }
            }
        }

        private void EndTimeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var endTimeCombBox = GetEndTimeComboBox();

            Calendar.EndTime = null;
            Calendar.EndTimeDisplay = string.Empty;

            if (endTimeCombBox != null)
            {
                if (endTimeCombBox.SelectedItem != null && endTimeCombBox.SelectedItem is Time)
                {
                    Time time = (Time)endTimeCombBox.SelectedItem;

                    var selectedTime = GetSelectedTime(time.Value);

                    Calendar.EndTime = time.Value;

                    if (selectedTime != null)
                        Calendar.EndTimeDisplay = selectedTime.Text;
                }
            }
        }

        private Time GetSelectedTime(int timeValue)
        {
            if (_times != null)
            {
                return (from time in _times
                        where time.Value == timeValue
                        select time).FirstOrDefault();
            }
            else
                return null;
        }

        private ComboBox GetTimeZoneComboBox()
        {
            return GetComboBoxInDataForm("TimeZoneComboBox");
        }

        private ComboBox GetStartTimeComboBox()
        {
            return GetComboBoxInDataForm("StartTimeComboBox");
        }

        private ComboBox GetEndTimeComboBox()
        {
            return GetComboBoxInDataForm("EndTimeComboBox");
        }

        private void EnablePublicCanBookCheckBox(bool enable)
        {
            var checkBox = GetCheckBoxInDataForm("PublicCanBookCheckBox");

            if (checkBox != null)
                checkBox.IsEnabled = enable;
        }

        private void EnableTimeframeComboBoxes(bool enable)
        {
            var startTimeComboBox = GetStartTimeComboBox();
            var endTimeComboBox = GetEndTimeComboBox();

            if (startTimeComboBox != null)
                startTimeComboBox.IsEnabled = enable;

            if (endTimeComboBox != null)
                endTimeComboBox.IsEnabled = enable;
        }

        private void ResetTimeframeComboBoxesItemsSource()
        {
            var startTimeComboBox = GetStartTimeComboBox();
            var endTimeComboBox = GetEndTimeComboBox();

            if (startTimeComboBox != null)
                startTimeComboBox.ItemsSource = null;

            if (endTimeComboBox != null)
                endTimeComboBox.ItemsSource = null;
        }

        private ComboBox GetComboBoxInDataForm(string name)
        {
            return (ComboBox)newCalendarForm.FindNameInContent(name);
        }

        private CheckBox GetCheckBoxInDataForm(string name)
        {
            return (CheckBox)newCalendarForm.FindNameInContent(name);
        }
    }
}